package com.homework.dao;

import java.io.BufferedWriter;
import java.io.IOException;
import java.sql.ResultSet;

import com.homework.util.connDB;
import com.sina.sae.storage.SaeStorage;

public class EvaluationDao {
	private static EvaluationDao evaluationDao = null;
	private connDB conn = new connDB();
	private ResultSet rs;

	public static EvaluationDao getInstance() {
		if (evaluationDao == null) {
			evaluationDao = new EvaluationDao();
		}
		return evaluationDao;
	}

	private String getDir(){
		return "d:/Evaluation11112.txt";
	}

	public boolean saveEvaluation(String json,String homeworkId)
	{

		BufferedWriter bw = null;
		String dir = null;

		String sql = "select f_track from t_homework where f_homeworkId = "+ homeworkId;
		ResultSet rs = conn.executeQuery(sql);
		try {
			if(rs.next())
			{
				dir = rs.getString("f_track");
			}
			if(dir != null&&dir.trim() != "")
			{
				SaeStorage ss = new SaeStorage(connDB.getAccessKey(),connDB.getSecretKey(),connDB.getAppName());
				ss.write("homework",dir,json);
				return true;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			conn.close();
			try {
				bw.close();
				//br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return false;
	}

	private String getDir(String homeworkId){
		String dir = "d:/test11112.txt";	
		try {
			String sql = "select f_track from t_homework where f_homeworkId=" + homeworkId;
			rs = conn.executeQuery(sql);
			if (rs.next()) {
				dir = (rs.getString("f_track"));
				rs.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			conn.close();
		}
		return dir;
	}

	public String getEvaluation(String homeworkId)
	{
		String dir = getDir(homeworkId);
		SaeStorage ss = new SaeStorage(connDB.getAccessKey(),connDB.getSecretKey(),connDB.getAppName());
		byte[] tmp = ss.read("homework", dir);
		return new String(tmp);

	}
}
